package com.http;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.http.client.RequestProcessor;
import com.http.method.Request;

import java.util.Iterator;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by mac_wk on 14-11-19.
 */
public class RequestQueue {
    //添加请求
    private static final int ADD_REQUEST = 1;
    private  static RequestQueue requestQueue;
    private final PriorityBlockingQueue<Request<?>> mNetworkQueue =
            new PriorityBlockingQueue<Request<?>>(20);
    private KNetWork kNetWork;
    private static Handler handler = null;
    private int processor=3;
    private RequestQueue() {
        kNetWork = KNetWork.getInstance(mNetworkQueue);
        RequestProcessor [] processors = new RequestProcessor[processor];
        for (RequestProcessor requestProcessor:processors)
        {
            requestProcessor = new RequestProcessor(mNetworkQueue);
            requestProcessor.start();

        }
    }
    public static RequestQueue getInstance()
    {
        if (requestQueue==null)
            requestQueue = new RequestQueue();
        return requestQueue;
    }
    public <T> PriorityBlockingQueue<Request<?>> addRequest(Request<T> request)
    {
        mNetworkQueue.add(request);
        return mNetworkQueue;

    }

    public void cancleAll()
    {
        Iterator<Request<?>> iterator = mNetworkQueue.iterator();
        while (iterator.hasNext())
        {
            Request request  =  iterator.next();
            request.setCancled(true);
        }
    }


}
